package com.one.consumer.main.ui.binder;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.one.common.config.OrderState;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.utils.StringUtils;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.one.common.view.widget.FlowLayout;
import com.one.consumer.R;
import com.one.consumer.common.state.OrderStateHandle;
import com.one.consumer.main.model.item.OrderItem;

import java.util.List;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/21 下午4:05
 * description ：订单列表项
 **/
public class OrderBinder extends BaseItemBinder<OrderItem> {
    public OrderBinder(OrderClickListener orderClickListener) {
        super(R.layout.item_order);
        this.orderClickListener = orderClickListener;
    }

    @Override
    protected void bindView(@NonNull BaseViewHolderMulti holder, @NonNull OrderItem item) {
        if (StringUtils.isNotBlank(item.getService_start_time()) && StringUtils.isNotBlank(item.getService_end_time())) {
            String time = item.getService_start_time().substring(0, 10);
            String timeArea = item.getService_start_time().substring(11, 16) + "-" + item.getService_end_time().substring(11, 16);
            holder.setText(R.id.tv_time, "服务时间：" + time + "  " + timeArea);
        }
        holder.setText(R.id.tv_location, "服务地址：" + item.getMaintailOrderAddressBean().getAddress());
        holder.getView(R.id.rl_order_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterManager.getInstance().go(RouterPath.ORDER_DETAIL, new DefaultExtra(item.getOrder_id() + ""));
            }
        });
        FlowLayout flowLayout = holder.getView(R.id.fl_keyword);
        if (StringUtils.isNotBlank(item.getParts_names())) {
            List<String> list = ListUtils.arrayToList(item.getParts_names().split(","));
            flowLayout.setViews(list);
        }
        setStateText(item, holder.getView(R.id.order_state));

        // 按钮
        TextView btn1 = holder.getView(R.id.tv_btn_1);
        TextView btn2 = holder.getView(R.id.tv_btn_2);
        TextView btn3 = holder.getView(R.id.tv_btn_3);
        OrderStateHandle.setStateView(item, btn1, btn2, btn3);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (orderClickListener != null) {
                    orderClickListener.cancelOrder(item);
                }
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (orderClickListener != null) {
                    orderClickListener.deleteOrder(item);
                }
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (orderClickListener == null) return;
                switch (item.getOrder_state()) {
                    case OrderState.WAIT_VISIT: // 待上门
                        orderClickListener.onDetail(item);
                        break;
                    case OrderState.WAIT_CONFIRM: // 等待确认服务
                        orderClickListener.onConfirm(item);
                        break;
                    case OrderState.WAIT_PAY: // 待付款
                        orderClickListener.onPay(item);
                        break;
                    case OrderState.PAY_END: // 已付款
                        orderClickListener.onEvaluation(item);
                        break;
                    default:
                        orderClickListener.onDetail(item);
                        break;
                }
            }
        });
    }

    /**
     * //0:已取消 1:待指派  2:待上门  3:等待确认服务  4:待付款  5:已付款
     *
     */
    public void setStateText(OrderItem item, TextView tvState) {
        String state = item.getOrder_state();
        String text = "";
        switch (state) {
            case OrderState.WAIT_ASSIGNED:
                text = "待指派师傅";
                break;
            case OrderState.WAIT_VISIT:
                text = "等待上门服务";
                break;
            case OrderState.WAIT_CONFIRM:
                text = "等待确认服务";
                break;
            case OrderState.WAIT_PAY:
                text = "等待付款";
                break;
            case OrderState.PAY_END:
                if (item.getAssessment_state().equals("1")) {
                    text = "未评价";
                } else if (item.getAssessment_state().equals("2")) {
                    text = "已评价";
                } else {
                    text = "已完成";
                }
                break;
            case OrderState.CANCEL:
                text = "已取消";
                break;
            default:
                text = "待指派师傅";
                break;
        }
        tvState.setText(text);
    }


    private OrderClickListener orderClickListener;

    public interface OrderClickListener {
        void cancelOrder(OrderItem item);

        void deleteOrder(OrderItem item);

        void onPay(OrderItem item);

        void onDetail(OrderItem item);

        void onConfirm(OrderItem item);

        void onEvaluation(OrderItem item);

    }

}
